package com.neusoft.hifly.information.article.api;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.hifly.commons.lang.LongUtil;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.core.token.TokenContextHolder;
import com.neusoft.hifly.core.utils.GlobalConstants;
import com.neusoft.hifly.information.article.entity.ArticleObj;
import com.neusoft.hifly.information.article.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Title: 文章资讯服务-文章资讯
 * <p>
 * Description: 文章 接口
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@CrossOrigin
@RestController
@RequestMapping("/api/v1/article/show")
@Api(value = "article-show-api", description = "文章查看接口")
public class ArticleShowApi {
	@Autowired(required = false)
	private IArticleService articleService;

	/**
	 * 通过逻辑ID获取文章
	 *
	 * @param HttpServletRequest
	 *            request
	 * @return 文章信息
	 */
	@ApiOperation("通过逻辑ID获取文章")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "logicId", value = "文章逻辑ID", dataType = "long", paramType = "path", required = true) })
	@GetMapping(value = "/{logicId}")
	public ApiResponse<?> getByLogicId(@PathVariable("logicId") final long logicId) {
		final String organCode = TokenContextHolder.get().getOrganCode();
		final long organLogicId = TokenContextHolder.get().getOrganLogicId();

		if (StringUtils.isEmpty(organCode) || LongUtil.isEmpty(organLogicId) || LongUtil.isEmpty(logicId)) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误!");
		}
		final ArticleObj obj = articleService.getByLogicId(organCode, organLogicId, logicId);
		if (obj == null) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "数据不存在！");
		}
		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！", obj);
	}

	/**
	 * 通过栏目分页显示文章列表
	 *
	 * @param HttpServletRequest
	 *            request
	 * @return 集合
	 */
	@ApiOperation("通过栏目分页显示文章列表")
	@ApiImplicitParams({ @ApiImplicitParam(name = "offset", value = "开始索引", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "limit", value = "每页纪录数", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "columnLogicId", value = "当前栏目ID", dataType = "String", paramType = "query", required = true) })
	@GetMapping(value = "/column")
	public ApiResponse<?> searchByColumn(final HttpServletRequest request) {
		final String organCode = TokenContextHolder.get().getOrganCode();
		final long organLogicId = TokenContextHolder.get().getOrganLogicId();

		if (StringUtils.isEmpty(organCode) || LongUtil.isEmpty(organLogicId)) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误!");
		}

		final String columnLogicId = StringUtils.trimToEmpty(request.getParameter("columnLogicId"));

		if (StringUtils.isEmpty(columnLogicId)) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "未指定栏目ID!");
		}

		final String offsetStr = StringUtils.trimToEmpty(request.getParameter("offset"));
		final String limitStr = StringUtils.trimToEmpty(request.getParameter("limit"));
		int offset = GlobalConstants.PAGE_OFFSET;
		int limit = GlobalConstants.PAGE_LIMIT;
		if (StringUtils.isNotEmpty(offsetStr) && StringUtils.isNumeric(offsetStr)) {
			offset = Integer.parseInt(offsetStr);
		}
		if (StringUtils.isNotEmpty(limitStr) && StringUtils.isNumeric(limitStr)) {
			limit = Integer.parseInt(limitStr);
		}

		// 参数
		final Map<String, Object> params = new HashMap<String, Object>(0);
		params.put("columnLogicId", columnLogicId);

		final Map<String, Object> page = articleService.searchByColumn(organCode, organLogicId, offset, limit, params);

		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！", page);
	}

}
